package com.kaibai.service.impl;

import com.kaibai.dao.UserDao;
import com.kaibai.dao.impl.UserDaoImpl;
import com.kaibai.pojo.Good;
import com.kaibai.pojo.Page;
import com.kaibai.pojo.User;
import com.kaibai.service.UserService;
import com.kaibai.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    public int insert(User user){
        Connection conn = null;
        int i = -1;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            i = dao.insert(conn,user);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                i = -1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return i;
    }

    @Override
    public int deleteById(int id) {
        Connection conn = null;
        int i = -1;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            i = dao.deleteById(conn,id);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                i = -1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return i;
    }

    @Override
    public int update(User user) {
        Connection conn = null;
        int i = -1;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            i = dao.update(conn,user);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                i = -1;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return i;
    }

    @Override
    public User getUserById(int id) {
        Connection conn = null;
        User user = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            user = dao.getUserById(conn,id);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                user = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return user;
    }

    @Override
    public List<User> getAll() {
        Connection conn = null;
        List<User> list = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            list = dao.getAll(conn);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                list = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return list;
    }

    @Override
    public boolean login(String name, String password) {
        Connection conn = null;
        User user = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            user = dao.login(conn,name,password);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                user = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return user!=null;
    }

    @Override
    public boolean existsUsername(String name) {
        Connection conn = null;
        User user = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            user = dao.existsUsername(conn,name);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                user = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return user!=null;
    }

    @Override
    public User getUserByName(String name) {
        Connection conn = null;
        User user = null;
        try{
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            user = dao.getUserByName(conn,name);
            conn.commit();
        }catch (Exception e){
            try {
                if (conn != null) {
                    conn.rollback();
                }
                user = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.close(conn,null);
        }
        return user;
    }

    @Override
    public Page<User> page(int pageNo, int pageSize) {
        Connection conn = null;
        Page<User> page = null;
        try {
            conn = JDBCUtil.getConnection();
            conn.setAutoCommit(false);
            page = new Page<>();
            page.setPageSize(pageSize);
            Integer pageTotalCount = dao.getForPageTotalCount(conn);
            page.setPageTotalCount(pageTotalCount);
            Integer pageTotal = pageTotalCount / pageSize;
            if(pageTotalCount % pageSize > 0){
                pageTotal++;
            }
            page.setPageTotal(pageTotal);

            page.setPageNo(pageNo);
            int begin = (page.getPageNo() - 1) * pageSize;
            List<User> list = dao.getForPageItems(conn,begin,pageSize);
            page.setItems(list);
            conn.commit();
        } catch (SQLException throwables) {
            try {
                if (conn != null) {
                    conn.rollback();
                }
                page = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(conn,null);
        }
        return page;
    }
}
